<?php
/**
 * http 服务
 */

$http = new Swoole\Http\Server('0.0.0.0', 9502);
//$http = new swoole_http_server('0.0.0.0', 9502);

//$http->on('Request', function ($request, $response){
//    if ($request->server['path_info'] == '/favicon.ico' || $request->server['request_uri'] == '/favicon.ico') {
//        $response->end();
//        return;
//    }
//
//    var_dump($request->get, $request->post);
//    $response->header('Content-Type', 'text/html;charset=utf-8');
//    $response->end('<h1> Hello World. #'.rand(100, 9999).'</h1>');
//});
//
//$http->on('Request', function ($request, $response) {
//    list($controller, $action) = explode('/', trim($request->server['request_uri'], '/'));
//    //根据 $controller, $action 映射到不同的控制器类和方法
//    (new $controller)->$action($request, $response);
//});

// 服务器启动时返回响应
$http->on('start', function ($http){
    echo "Swoole http server is started at http://127.0.0.1:9502\n";
});

// 向服务器发送请求是返回响应
$http->on('request', function ($request, $response){
    $response->header('Content-type', 'text/plain');
    $response->end("Hello World\n");
});

// 启动
$http->start();